package com.geodroid.action;

import java.util.ArrayList;

import android.graphics.RectF;
import android.util.Log;

import com.geodroid.common.kernel.algos.AlgoMidPoint;
import com.geodroid.common.kernel.geos.GeoPoint2;
import com.geodroid.common.kernel.geos.GeoSegment;
import com.geodroid.euclidian.DrawPoint;
import com.geodroid.euclidian.Drawable;
import com.geodroid.euclidian.EuclidianView;

public class ActionCreateMiddle extends Action {

	private GeoPoint2 p;

	
	public ActionCreateMiddle(int nbC) {
		super(nbC) ;  
	}
	
	/* (non-Javadoc)
	 * @see com.geodroid.action.Action#process(int, float, float, java.util.ArrayList)
	 */
	@Override
	public void process(RectF touchedArea, EuclidianView view) {
		AlgoMidPoint algo = new AlgoMidPoint((GeoPoint2) draSel.get(0).getGeo(),(GeoPoint2) draSel.get(1).getGeo()) ;
		this.p = algo.getPoint() ;
		view.addPoint(p) ; 
	}

	/* (non-Javadoc)
	 * @see com.geodroid.action.Action#processDown(android.graphics.RectF, com.geodroid.euclidian.EuclidianView)
	 */
	@Override
	protected void processDown(RectF touchedArea, EuclidianView view) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see com.geodroid.action.Action#processMove(android.graphics.RectF, com.geodroid.euclidian.EuclidianView)
	 */
	@Override
	protected void processMove(RectF touchedArea, EuclidianView view) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see com.geodroid.action.Action#processUp(android.graphics.RectF, com.geodroid.euclidian.EuclidianView)
	 */
	@Override
	protected void processUp(RectF touchedArea, EuclidianView view) {
		this.process(touchedArea, view) ; 
		
	}

	/* (non-Javadoc)
	 * @see com.geodroid.action.Action#add(com.geodroid.euclidian.Drawable, java.util.ArrayList)
	 */
	@Override
	public void add(Drawable d, ArrayList<Drawable> result) {
		Log.d("ActionCreateMiddle", "<add>")  ;
		if (d.getGeo() instanceof GeoSegment){
			Log.d("ActionCreateMiddle", "GeoSegment add dans draSel de taille" + result)  ;
			Drawable d1 = new DrawPoint(  ( (GeoSegment) d.getGeo()).getStartPoint(),new EuclidianView()) ; 
			Drawable d2 = new DrawPoint( ( (GeoSegment) d.getGeo()).getEndPoint(),new EuclidianView()) ;
			result.clear() ;
			d1.highLight() ; 
			d2.highLight() ;
			result.add(d1) ; 
			result.add(d2) ;
		}else{
			d.highLight() ;
			result.add(d) ;
		}
		Log.d("ActionCreateMiddle", "</add>")  ;
	}

	@Override
	public boolean validate(Drawable d) {
		return (d.getGeo()) instanceof GeoPoint2 || (d.getGeo() instanceof GeoSegment )  ;
	}

}
